<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Example extends Admin_Controller {
	private $webmaster_config = 'config';
	private $model_config     = 'example';
    public function __construct(){
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('image_lib');
        $this->load->model('category');
		$this->set_webmaster_config(); //添加后台配置文件
    }
	
	private function set_webmaster_config(){
		$model=$this->uri->segment(1);
		$this->webmaster_config=$this->config->item('webmaster');
		$this->model_config=$this->config->item($model); //example
	}
	
    public function category_list(){
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
    	$page_name	= 'category';
        $uid     	= '0,';
        $where  	= "1=1";
        $where 	   .= " and uid='".$uid."'";
        $list   	= $this->category->output_category_ary('ca_example_category',"*",$where);
        $data  	 	= array(
            'list'  			=> $list,
            'page_name'			=> $page_name,
            'model_config'  	=> $model_config,
            'webmaster_config'	=> $webmaster_config
        );
        $this->load->view(THEME_PATH.'/example.php',$data);
    }
	
    public function category_mod(){
    	$page_name	 	  = 'category';	
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
        $cate_id     	  = (int)$_GET['cate_id'];
        $item        	  = $this->data->get_one('ca_example_category',"cate_id='$cate_id'");
        $path        	  = $this->category->path($cate_id,'ca_example_category');
        if($cate_id){
            $o_dept=$item['dept']-($this->data->get_max('ca_example_category', "uid like '{$item['uid']}{$item['cate_id']},%'", 'dept')-$model_config['category_dept']);
            $e_where="cate_id!='{$item['cate_id']}' and dept<".($item['sub_cate']?$o_dept:$model_config['category_dept']);
            $ouput_select=$this->category->output_category_select('under_cate_id',$this->category->get_cate_id($item['uid']), 'ca_example_category', "uid='0,' and ".$e_where, $e_where,"选择框");
        } else {
            $ouput_select=$this->category->output_category_select('under_cate_id',$_SESSION['webmaster']['cate_id'], 'ca_example_category', 'uid="0,"', "dept<".$model_config['category_dept'], "选择框");
        }
        $data=array(
            'cate_id'       		=> $cate_id,
            'ouput_select'  		=> $ouput_select,
            'item'          		=> $item,
            'path'          		=> $path,
            'page_name'				=> $page_name,
            'model_config'  		=> $model_config,
            'webmaster_config'		=> $webmaster_config
        );
        $this->load->view(THEME_PATH.'/example.php',$data);
    }

    public function info_list(){
	    $page_name	 	  = 'info';	
	    $webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
		//-------翻页 start------//
		$where = "1=1";
		$page_count=$model_config['example_page_count'];
		$row_count=$this->data->get_row_count('ca_example', $where);
		$total_pages=ceil($row_count/$page_count);
		$page=(int)$_GET['page'];
		$page<1 && $page=1;
		$page>$total_pages && $page=1;
		$start_row=($page-1)*$page_count;
		$query_string=base_url('/example/info_list/').'?'.query_string('page').'&page=';
		$turn_page = turn_page($page, $total_pages, $query_string, $row_count, 'Previous', 'Next',3,'style="margin:0px; text-align:right;"');
		$list = $this->data->get_limit('ca_example',$where,"*","my_order desc,eid desc", $start_row, $page_count);
		//-------翻页 end------//
		//新闻分页
		$data=array(
			'model_config' 		=> $model_config,
			'page_name'			=> $page_name,
			'webmaster_config'	=> $webmaster_config,
			'list'				=> $list,
			'turn_page'			=> $turn_page
		);	
        $this->load->view(THEME_PATH.'/example.php',$data);
    }

    public function info_mod(){
    	$page_name	 	  = 'info';	
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
        $example_config	  = $this->config->item('example');
        $eid      	  = (int)$_GET['eid'];	
		$item		  	  = $this->data->get_one('ca_example',"eid='$eid'");
		$contents		  = $this->data->get_value('ca_example_contents',"eid='$eid'",'contents');
		$cate_id		  = $item['cate_id'];
    	$ouput_select 	  = $this->category->output_category_select('cate_id', $cate_id, 'ca_example_category', 'UId="0,"', 1,"选择框");
		$data=array(
            'eid'       	=> $eid,
            'ouput_select'  	=> $ouput_select,
            'item'          	=> $item,
            'contents'			=> $contents,
            'model_config'     	=> $model_config,
            'webmaster_config'	=> $webmaster_config,
            'page_name'			=> $page_name,
            'root'				=> $root
        );
        $this->load->view(THEME_PATH.'/example.php',$data);
    }
	public function info_mod_action(){
		$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;	
		$data	 		  = get_data($this->input->post(),'');
		$cate_id		  = $this->input->post('cate_id',true);
		$eid 		  	  = $this->input->post('eid');
		$picpath_ary	  = $this->input->post('mul_upload',true);
        $item			  = $this->data->get_one('ca_example',"eid='$eid'");
		$contents         = $_POST['contents'];
		$this->form_validation->set_rules('name-mod', '标题名称', 'required');
        !$this->form_validation->run() && e_json(validation_errors(),0);
		//upload and resize start
        $ret_ary     = array();
		for($i=0;$i<$model_config['example_upload_num'];$i++){
			if($item['picpath_'.$i]!=$picpath_ary[$i]){
				$new_file=@upload($picpath_ary[$i],'example',$webmaster_config);
				if($model_config['example_thumb_size']) { @resize($new_file,$webmaster_config,$model_config['example_thumb_size']); }
				$ret_ary['picpath_'.$i]=$new_file;
			} else {
				$ret_ary['picpath_'.$i]=$picpath_ary[$i];
			}
		}
		//upload and resize end
		$data=array_merge($data,(array)$ret_ary);
		$data['cate_id']=$cate_id;
		$data['acctime']=time();
		if($eid){
			$this->data->update('ca_example',"eid='$eid'",$data);
			$this->data->update('ca_example_contents',"eid='$eid'",array("contents"=>$contents));
			!$this->data->get_row_count('ca_example_contents',"eid='$eid'") && $this->data->insert('ca_example_contents',array("contents"=>$contents,'eid'=>$eid));
		} else {
			$this->data->insert('ca_example',$data);
			$insert_id=$this->db->insert_id();
			$this->data->insert('ca_example_contents',array('contents'=>$contents,'eid'=>$insert_id));
		}		
		e_json('',1);
	}
	
    public function category_mod_action(){
        $webmaster_config =   $this->webmaster_config;
		$model_config 	  =   $this->model_config;
        $category    	  =   $this->input->post('category',true);
        $uid         	  =   $this->input->post('uid',true);
        $cate_id     	  =   $this->input->post('cate_id',true);
        $undercateid 	  =   $this->input->post('under_cate_id',true);
        $picpath_ary	  =   $this->input->post('mul_upload',true);
        $item			  =   $this->data->get_one('ca_example_category',"cate_id='$cate_id'");
        $this->form_validation->set_rules('category', '分类名称', 'required');
        !$this->form_validation->run() && e_json(validation_errors(),0);
        //upload and resize start
        $ret_ary     = array();
		for($i=0;$i<$model_config['category_upload_num'];$i++){
			if($item['picpath_'.$i]!=$picpath_ary[$i]){
				$new_file=@upload($picpath_ary[$i],'example',$webmaster_config);
				if($model_config['category_thumb_size']) { @resize($new_file,$webmaster_config,$model_config['category_thumb_size']); }
				$ret_ary['picpath_'.$i]=$new_file;
			} else {
				$ret_ary['picpath_'.$i]=$picpath_ary[$i];
			}
		}
		//upload and resize end 
        if(!$undercateid){
            $uid='0,';
            $dept=1;
        } else {
            $uid  = $this->category->get_uid($undercateid, 'ca_example_category');
            $dept = substr_count($uid, ',');
        }
        $array = array(
            'uid'			=>	$uid,
            'category'		=>	$category,
            'dept'          =>	$dept
        );
        $array=array_merge($array,(array)$ret_ary);
        if($cate_id){
            $this->data->update('ca_example_category',"cate_id='$cate_id'",$array);
        } else {
            $this->data->insert('ca_example_category',$array);
        }
        e_json('',1);
    }
    public function category_img_del(){
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
		$num			  = (int)$this->input->post('num',true);
        $cate_id     	  = (int)$this->input->post('id',true);
        $where       	  = "cate_id='$cate_id'";
        del_img('ca_example_category',$this->data,$model_config['category_thumb_size'],$where,'picpath_'.$num,$root);
        e_json('',1);
    }
	public function info_img_del(){
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
		$num			  = (int)$this->input->post('num',true);
        $eid     	  = (int)$this->input->post('id',true);
        $where       	  = "eid='$eid'";
        del_img('ca_example',$this->data,$model_config['example_thumb_size'],$where,'picpath_'.$num,$root);
        e_json('',1);
    }
	
	public function category_order(){
		$list_id		= $this->input->post('list_id',true);
		$list_val		= $this->input->post('list_val',true);
		$db_name		= $this->input->post('db_name',true);
		$list_id_ary 	= explode(',', $list_id);
		$list_val_ary	= explode(',', $list_val);
		for($i=0;$i<count($list_id_ary);$i++){
			$this->data->update('ca_'.$db_name,"cate_id='{$list_id_ary[$i]}'",array('my_order'=>$list_val_ary[$i]));
		}
		e_json('',1);
	}
	public function list_order(){
		$list_id		= $this->input->post('list_id',true);
		$list_val		= $this->input->post('list_val',true);
		$db_name		= $this->input->post('db_name',true);
		$list_id_ary 	= explode(',', $list_id);
		$list_val_ary	= explode(',', $list_val);
		for($i=0;$i<count($list_id_ary);$i++){
			$this->data->update('ca_'.$db_name,"eid='{$list_id_ary[$i]}'",array('my_order'=>$list_val_ary[$i]));
		}
		e_json('',1);
	}
	
	public function group_del(){
		$group_id	= $this->input->post('group_id',true);
		$ary		= explode('|', $group_id);
		foreach((array)$ary as $k=>$v){
			$this->info_del_action($v);
		}
		e_json('',1);	
	}
	
	public function list_del_action(){
		$eid    	  = (int)$this->input->post('id',true);
		$this->info_del_action($eid);
        e_json('',1);
	}
	
	private function info_del_action($id){
		$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
        $eid    	  = $id;
		$where      	  = "eid='$eid'";
		for($i=0;$i<$model_config['example_upload_num'];$i++){
        	del_img('ca_example',$this->data,$model_config['example_thumb_size'],$where,"picpath_".$i,$root);	
        }
        $this->data->delete('ca_example',$where);
	}
	
    public function category_del_action(){
        $webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
        $cate_id    	  = (int)$this->input->post('id',true);
        $where      	  = "cate_id='$cate_id'";
        $category   	  = $this->data->get_one('ca_example_category',"cate_id='$cate_id'");
        if($this->data->get_row_count('ca_example_category',"uid like '%,".$cate_id.",%'")){
            e_json('该分类下存在子分类',0);
        }
        for($i=0;$i<$model_config['category_upload_num'];$i++){
        		del_img('ca_example_category',$this->data,$model_config['category_thumb_size'],$where,"picpath_".$i,$root);	
        }
        $this->data->delete('ca_example_category',$where);
        e_json('',1);
    }
}
?>